<template>
  <edu-dialog
    :title.sync="dialog.title"
    :visible.sync="dialog.showDialog"
    @close="close"
    @confirm="submit"
    width="70%"
  >
    <el-input
      placeholder="请输入内容"
      v-model="queryParams.condition.title"
      size="small"
      clearable
    >
      <template slot="append"
        ><el-button
          @click="
            () => {
              query();
            }
          "
          size="small"
          >搜索</el-button
        ></template
      >
    </el-input>

    <edu-table
      ref="tableList"
      :data="tableList"
      :pageSize="queryParams.pageSize"
      :pageNum="queryParams.pageNum"
      :total="queryParams.total"
      @page-change="handleCurrentChange"
      :order="true"
      :selection="true"
    >
      <el-table-column label="农产品名称" prop="title" />
      <el-table-column label="价格" prop="price" :formatter="rounding" />
      <el-table-column label="农产品类型" prop="produceTypeName" />
      <el-table-column label="供应商" prop="providerName" />
      <el-table-column label="卖点" prop="author" />
      <el-table-column label="创建时间" prop="pushTime" width="160" />
    </edu-table>
  </edu-dialog>
</template>

<script>
import { pageFarmProduce } from "@/api/agricultureProducts";
import utils from "@/utils";

export default {
  props: ["value"],
  data() {
    return {
      queryParams: {
        condition: {
          title: "",
          pushStatus: 1, //已发布
        },
        pageNum: 1,
        pageSize: 10,
        total: 0,
      },
      tableList: [],
      dialog: {
        title: "查看产品",
        showDialog: false,
      },
    };
  },
  methods: {
    open() {
      this.dialog.showDialog = true;
      this.query(1);
    },
    close() {
      this.dialog.showDialog = false;
    },
    handleCurrentChange(page) {
      this.queryParams.pageSize = page.pageSize;
      this.queryParams.pageNum = page.pageNum;

      this.query();
    },
    async query(pageNum) {
      // 有pageNum 重置为第一页
      if (pageNum) {
        this.queryParams.pageNum = pageNum;
      }

      let { returnObj } = await pageFarmProduce(this.queryParams);

      this.tableList = returnObj.list;
      this.queryParams.total = returnObj.total;
      this.queryParams.pageSize = returnObj.pageSize;
    },
    // 获取id
    async submit() {
      let selections = this.$refs.tableList.getCheckData();
      this.close();

      this.$emit("done", selections);
    },
    rounding(row, column) {
      return parseFloat(row[column.property]).toFixed(2);
    },
  },
};
</script>

<style lang="scss" scoped>
.buttons {
  justify-content: flex-end;
}
</style>
